
import { _decorator, Component, sys } from 'cc';
import { Camera3DCtl } from './Camera3DCtl';
import { EventMgr } from './framework/manager/EventMgr';
import { UIMgr } from './framework/manager/UIMgr';
import { CacheUtils } from './framework/utils/CacheUtils';
import { PowerRecoveryMgr } from './game/common/PowerRecoveryMgr';
import { EventName, UIID } from './game/config/Config';
import { GameDef } from './game/gameLogic/GameDef';
import { NetworkUtils } from './game/manager/NetworkUtils';
import { PlayerMgr } from './game/manager/PlayerMgr';
const { ccclass, property } = _decorator;
@ccclass('Main')
export class Main extends Component {
    @property({ type: Camera3DCtl, displayName: '' })
    mainCamera: Camera3DCtl = null;

    public static instance: Main;
    onLoad() {
        this.loadToPool();
        // PhysicsSystem.instance.enable = true;
        // PhysicsSystem2D.instance.enable = true;
        Main.instance = this;
        UIMgr.getInstance().openUI(UIID.UITouch, UIID.Default)
        let loadSceneName = PlayerMgr.getInstance().getUserData().getLevel() === 1 && !PlayerMgr.instance.userCloudData;
        if (loadSceneName) {
            UIMgr.getInstance().openUI(UIID.UIHome, UIID.Default, PlayerMgr.getInstance().getUserData().getLevel())
        } else {
            UIMgr.getInstance().openUI(UIID.UIHome, UIID.Default)
        }
        UIMgr.getInstance().addNode(UIID.PREFAB_RibbonEffect, 100001)
        // UIMgr.getInstance().openUI(UIID.UIGame, UIID.Default)
        // UIMgr.getInstance().openUI(UIID.UITop, UIID.Default)
        this.getUserIp()
        PowerRecoveryMgr.instance.init();
        // HttpMgr.getInstance().saveUserData("asdsad", { level: 1, power: 10000 })
        PowerRecoveryMgr.instance.call((dt: number) => {
            if (PlayerMgr.instance.userData.data.roleHp >= PlayerMgr.instance.userData.data.maxRoleHp) return;
            PlayerMgr.instance.userData.data.addHpEndTime -= 1;
            PlayerMgr.instance.userData.setHpAddTime(PlayerMgr.instance.userData.data.addHpEndTime);
            EventMgr.getInstance().emit(EventName.UpdateHpTimer, PlayerMgr.instance.userData.data.addHpEndTime);
            if (PlayerMgr.instance.userData.data.addHpEndTime <= 0) {
                PlayerMgr.instance.userData.data.addHpEndTime = 600;
                PlayerMgr.instance.userData.addRoleHp(1)
            }
        }).start();
        CacheUtils.setPlayLevel(1)
    }
    start() {
        console.log("进入游戏界面~");
        //播放背景音乐
        // AudioMgr.getInstance().playMusic(AudioId.mainBgm, "MainBGM");
    }
    getUserIp() {
        NetworkUtils.getPublicIP().then((ip) => {
            GameDef.instance.proCode = ip.data?.proCode || "440000";
            GameDef.instance.cityName = ip.data?.pro || "广东省";
            console.warn("ip", GameDef.instance.proCode);
        })
    }
    loadToPool() {
        // ResourceMgr.getInstance().loadRes("object", "Coin", Prefab, (prefab: Prefab) => {
        //     let node = instantiate(prefab);
        //     PoolMgr.getInstance().creatrePool(node.name, node, 100);
        // });
    }

    //便利所有建筑
    searchMapNode() {
        return;
        console.log("生成json");
        let data = {};

        // 引用方式
        this.creatJosnFile(data, "mapJson");
    }


    /**生成json文件 */
    creatJosnFile(data: any, fileName: string) {
        var content = JSON.stringify(data)
        this.saveForBrowser(content, fileName)
    }

    /**
     * 存字符串内容到文件。
     * @param textToWrite  - 要保存的文件内容
     * @param fileNameToSaveAs - 要保存的文件名
     */
    saveForBrowser(textToWrite, fileNameToSaveAs) {
        if (sys.isBrowser) {
            console.log("浏览器");
            let textFileAsBlob = new Blob([textToWrite], { type: 'application/json' });
            let downloadLink = document.createElement("a");
            downloadLink.download = fileNameToSaveAs;
            downloadLink.innerHTML = "Download File";
            if (window.webkitURL != null) {
                downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
            }
            downloadLink.click();
        }
    }

}
